package knowlwdge_graph.knowledge_etl.yago_etl;

import common.dao.postgre.PostGreJDBCDao;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * Created by julianzliu on 4/17/2017.
 */
public class TransformerF2OMultiThread {

    static final org.slf4j.Logger logger = LoggerFactory.getLogger(TransformerF2OMultiThread.class);

    //private PostGreJDBCDao postGreJDBCDao;
    //private Connection conn;
    private ExtractorTSVFiles extractorTSVFiles;
    private Integer thread_num;

    public TransformerF2OMultiThread(Integer thread_num){
        //this.postGreJDBCDao = new PostGreJDBCDao();
        //this.conn = this.postGreJDBCDao.getConn();
        this.thread_num = thread_num;
        this.extractorTSVFiles = new ExtractorTSVFiles();
        logger.info("[TransformerF2OMultiThread] thread_num = {}", this.thread_num);
    }


    public List<String> getAllFiles(String path){
        return this.extractorTSVFiles.getAllFiles(path);
    }


    public void createThread(int begin, int end, List<String> files){
        (new Thread( new TransformerF2O.TransformerThread(begin, end, files) )).start();
    }


    public void createMultiThread(List<String> files){

        int total_size = files.size();

        logger.info("[createMultiThread] total_size = {}\n\n", total_size);

        int per_thread_size = total_size/thread_num;
        int curr_size = 0;
        int curr_thread = 0;
        while(curr_size < total_size){

            int begin = curr_size;
            int end = Math.min(total_size, curr_size + per_thread_size);
            createThread( begin, end, files );
            curr_thread += 1;
            logger.info("[createMultiThread] curr_thread = {} \n\n ", curr_thread);

            curr_size = end;
        }
    }





    /*
    ******************************************************************
     */
    public static void main(String[] args){
        for(String arg:args){
            System.out.println(arg);
        }
        System.out.println("----------------------");
        String verbose = "schema";
        if(args.length >= 1)
            verbose = args[0];
        System.out.println("verbose = " + verbose);
        if(verbose.equalsIgnoreCase("schema")){
            SchemaConstructer constructer = new SchemaConstructer();
            constructer.createSchema();
        }
        else{
            int thread_num = 5;
            if(args.length >= 2)
                thread_num = Integer.parseInt(args[1]);
            System.out.println("thread_num = " + thread_num);
            String folder = "/home/gammaliu/Datas/yago/yago3/copy_to_postgres";

            TransformerF2OMultiThread multiThread = new TransformerF2OMultiThread( thread_num );
            List<String> files = multiThread.getAllFiles(folder) ;
            multiThread.createMultiThread( files );
        }





    }


}
